<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="测站编码" prop="stCd">
        <el-input
          v-model="queryParams.stCd"
          placeholder="请输入测站编码"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测站名称" prop="stName">
        <el-input
          v-model="queryParams.stName"
          placeholder="请输入测站名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测站类型" prop="stType">
        <el-input
          v-model="queryParams.stType"
          placeholder="请输入测站类型"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="false"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getPage"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="page" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="测站编码" prop="stCd" width="200"/>
      <el-table-column label="测站名称" prop="stName" :show-overflow-tooltip="true" width="240"/>
      <el-table-column label="测站类型" prop="stType" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="上报频率（分钟/条）" prop="submitFrequency" width="200"/>
      <el-table-column label="是否锁定" align="center" width="150">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.locked"
            :active-value=1 :inactive-value=0
            @change="handleLockedStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" align="center" width="150">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.valid"
            :active-value=0 :inactive-value=1
            @change="handleValidStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getPage"
    />

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :visible.sync="open" :title="title" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-col :span="24">
          <el-form-item label="测站编码" prop="stCd">
            <el-input v-model="form.stCd" placeholder="请输入测站编码" show-word-limit clearable
                      :style="{width: '100%'}" :disabled="dialogType === 'update'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="测站名称" prop="stName">
            <el-input v-model="form.stName" placeholder="请输入测站名称" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="测站类型" prop="stType">
            <el-input v-model="form.stType" placeholder="请输入测站类型" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="上报频率" prop="submitFrequency">
            <el-input v-model="form.submitFrequency" placeholder="请输入上报频率（分钟/条）" clearable
                      :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否有效" prop="valid" required>
            <el-switch v-model="form.valid" :active-value=0 :inactive-value=1></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否锁定" prop="locked" required>
            <el-switch v-model="form.locked" :active-value=1 :inactive-value=0></el-switch>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    page,
    getStation,
    insertStation,
    updateStation,
    delStation,
  } from "@/api/analysisconfigure/stationaccess";

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        stCds: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        page: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        dialogType: '',
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          stCd: undefined,
          stName: undefined,
          stType: undefined,
          submitFrequency: undefined,
          valid: undefined,
          locked: undefined,
        },
        // 表单校验
        rules: {
          stCd: [{
            required: true,
            message: '请输入测站编码',
            trigger: 'blur'
          }],
          stName: [{
            required: true,
            message: '请输入测站名称',
            trigger: 'blur'
          }],
          stType: [{
            required: true,
            message: '请输入测站类型',
            trigger: 'blur'
          }],
          submitFrequency: [{
            required: true,
            message: '请输入上报频率（分钟/条）',
            trigger: 'blur'
          }],
        },
      };
    },
    created() {
      this.getPage();
    },
    methods: {
      /** 查询分页列表 */
      getPage() {
        this.loading = true;
        page(this.queryParams).then(response => {
            this.page = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.stCds = selection.map(item => item.stCd)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 测站锁定状态修改
      handleLockedStatusChange(row) {
        let text = row.locked === 1 ? "锁定" : "解除锁定";
        this.$modal.confirm('确认要"' + text + '""' + row.stName + '"吗？')
          .then(function () {
            return updateStation(row);
          }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.locked = row.locked === 0 ? 1 : 0;
        });
      },
      // 测站有效状态修改
      handleValidStatusChange(row) {
        let text = row.valid === 0 ? "有效" : "无效";
        this.$modal.confirm('确认要"' + text + '""' + row.stName + '"吗？')
          .then(function () {
            return updateStation(row);
          }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.valid = row.valid === 1 ? 0 : 1;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          stCd: undefined,
          stName: undefined,
          stType: undefined,
          submitFrequency: undefined,
          valid: undefined,
          locked: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getPage();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.title = "新增测站";
        this.open = true;
        this.dialogType = "add";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const stCds = row.stCd || this.stCds;
        getStation(stCds).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改接入测站信息";
          this.dialogType = "update";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        let that = this;
        that.$refs["form"].validate(valid => {
          if (valid) {
            if ('update' === that.dialogType) {
              updateStation(that.form).then(response => {
                that.$modal.msgSuccess("修改成功");
                that.open = false;
                that.getPage();
              });
            } else {
              insertStation(that.form).then(response => {
                that.$modal.msgSuccess("新增成功");
                that.open = false;
                that.getPage();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const stCds = row.stCd || this.stCds;
        this.$modal.confirm('是否确认删除？').then(function () {
          return delStation(stCds);
        }).then(() => {
          this.getPage();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/user/export', {
          ...this.queryParams
        }, `user_${new Date().getTime()}.xlsx`)
      },
    }
  };
</script>
